<div>
  <h1><%= @product.new_record? ? "新建商品" : "修改商品 ##{params[:id]}" %></h1>
</div>

<div class="form-body">
  <%= form_for @product,
    url: (@product.new_record? ? admin_products_path : admin_product_path(@product)),
    method: (@product.new_record? ? 'post' : 'put'),
    html: { class: 'form-horizontal' } do |f| %>

    <% unless @product.errors.blank? %>
      <div class="alert alert-danger">
        <ul class="list-unstyled">
          <% @product.errors.messages.values.flatten.each do |error| %>
            <li><i class="fa fa-exclamation-circle"></i> <%= error %></li>
          <% end -%>
        </ul>
      </div>
    <% end -%>


    <div class="form-group">
      <label for="title" class="col-sm-2 control-label">名称:*</label>
      <div class="col-sm-5">
        <%= f.text_field :title, class: "form-control" %>
      </div>
    </div>
    <div class="form-group">
      <label for="brand" class="col-sm-2 control-label">品牌：</label>
      <div class="col-sm-5">
        <select name="product[brand_id]">
          <% Brand.all.each do |brand| %>
            <option value="<%= brand.id %>" <%= 'selected' if @product.brand_id == brand.id %>><%= brand.title %></option>
          <% end %>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="brand" class="col-sm-2 control-label">规格：</label>
      <div class="col-sm-5">
        <select name="product[spec_id]">
          <% Spec.all.each do |spec| %>
            <option value="<%= spec.id %>" <%= 'selected' if @product.spec_id == spec.id %>><%= spec.title %></option>
          <% end %>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="category_id" class="col-sm-2 control-label">所属分类:</label>
      <div class="col-sm-5">
        <% @root_categories.each do |category| %>
          <div>
            <label style="color: #777;"><%= category.title %>：</label>
            <% category.children.each do |sub_category| %>
              <input type="checkbox" value="<%= sub_category.id %>" <%= @product.category_ids.include?(sub_category.id.to_s) ? 'checked' : '' %> name="product[category_ids][]" id="category_ids_<%= sub_category.id %>" />
             <label for="category_ids_<%= sub_category.id %>">&nbsp;&nbsp;<%= sub_category.title %></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <% end %>
          </div>
        <% end %>
      </div>
    </div>
    <div class="form-group">
      <label for="brand" class="col-sm-2 control-label">商品类型：</label>
      <div class="col-sm-5">
        <select name="product[product_type]">
          <% Product.product_types.keys.each_with_index do |pt, index| %>
            <option value="<%= pt %>" <%= 'selected' if (@product.product_type == pt) || (pt == 'customized') %>><%= I18n.t("enums.product.product_type.#{pt.to_s}") %></option>
          <% end %>
        </select>
      </div>
    </div>

    <div class="form-group">
      <label for="title" class="col-sm-2 control-label">上下架状态:*</label>
      <div class="col-sm-5">
        <select name="product[status]">
          <% Product.statuses.keys.each_with_index do |row, index| %>
            <option value="<%= row %>" <%= 'selected' if @product.status == row %>><%= row %></option>
          <% end -%>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="amount" class="col-sm-2 control-label">库存*:</label>
      <div class="col-sm-5">
        <%= f.number_field :amount, class: "form-control", min: 0 %> 必须为整数
      </div>
    </div>
    <div class="form-group">
      <label for="amount" class="col-sm-2 control-label">库存单位:</label>
      <div class="col-sm-5">
        <select id="" name="product[stock][unit]">
          <% Stock.units.each do |k, v| %>
            <option value="<%= v %>" <%= 'selected' if @product.unit == k %>><%= @product.enum_label(k) %></option>
          <% end %>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="amount" class="col-sm-2 control-label">销量*:</label>
      <div class="col-sm-5">
        <%= f.number_field :sales, class: "form-control", min: 0 %> 必须为整数
      </div>
    </div>
    <div class="form-group">
      <label for="price" class="col-sm-2 control-label">价格*:</label>
      <div class="col-sm-5">
        <%= f.number_field :price, class: "form-control", min: 0 %>
      </div>
    </div>
    <div class="form-group">
      <label for="msrp" class="col-sm-2 control-label">MSRP*:</label>
      <div class="col-sm-5">
        <%= f.number_field :msrp, class: "form-control", min: 0 %>
      </div>
    </div>
    <div class="form-group">
      <label for="" class="col-sm-2 control-label">商品描述签名:</label>
      <div class="col-sm-5">
        <select id="" name="product[product_sign_id]">
          <% ProductSign.all.each do |sign| %>
            <option value="">不使用签名</option>
            <option value="<%= sign.id %>" <%= 'selected' if @product.product_sign_id == sign.id %>><%= sign.title %></option>
          <% end %>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="description" class="col-sm-2 control-label">Summary*:</label>
      <div class="col-sm-5">
        <%= f.text_area :summary, class: "form-control" %>
      </div>
    </div>
    <div class="form-group">
      <label for="description" class="col-sm-2 control-label">描述*:</label>
      <div class="col-sm-5">
        <%= f.text_area :desc, class: "form-control", id: 'editor-container' %>
      </div>
    </div>
      <div class="form-group">
        <label for="description" class="col-sm-2 control-label">商品属性: (可动态增加)</label>
          <div class="col-sm-5">
          <% properties = ProductProperty.find_or_init_properties(@product.id) %>
          <% properties.each do |property| %>
            <div class="property-unit">
              <input type="text" name="product[properties][][item_type]" value="<%= property.item_type %>" placeholder="请填写属性名" />
              <input type="text" name="product[properties][][item_value]" value="<%= property.item_value %>" placeholder="请填写属性值" />
              <input type="button" value="删除" @click="delExsitedProperty"/ >
              <br />
              <br />
            </div>
          <% end %>
          <div v-for="(prop, index) in properties">
            <input type="text" :name="prop.itemType" :placeholder="prop.itemTypePlaceholder" />
            <input type="text" :name="prop.itemValue" :placeholder="prop.itemValuePlaceholder" />
            <input type="button" value="删除" @click="delProperty(index)"/ >
            <br />
            <br />
          </div>
          </div>
          <div><input type="button" @click="addProperty" value="添加新属性"></div>
      </div>
    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-8">
        <%= f.submit (@product.new_record? ? "新建商品" : "编辑商品"), class: "btn btn-primary" %>
      </div>
    </div>
  <% end -%>
</div>
